package com.yc.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yc.entity.AttendanceSheet;
import org.apache.ibatis.annotations.*;

import java.rmi.MarshalledObject;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author mqm
 * @since 2023-07-27
 */
@Mapper
public interface AttendanceSheetMapper extends BaseMapper<AttendanceSheet> {

    /**
     * 查询学生个人的考勤信息
     * @param stuid
     * @return
     */
//    @Select("select d.*, x.`Auditstatus`,x.teacherReply from (SELECT t.id,s.coursename,s.date,s.classroom,a.`status` from\n" +
//            " attendance_records a ,attendance t , `schedule` s where a.attId = t.id and  t.scheduleId = s.id\n" +
//            " and a.stuid = #{stuid} AND a.`status` IN (3,4,5)) d LEFT JOIN attendance_sheet x on x.Attid = d.id and x.stuid = #{stuid} ORDER BY x.Auditstatus asc  ")
//    List<Map<String,Object>>queryStuAttendanceInfo(@Param("stuid") String stuid);

    List<Map<String, Object>> queryStuAttendanceInfo(@Param("stuid") String stuid);

    /**
     * 查询考勤表老师的ID
     * @param Attid
     * @return
     */
    @Select("select teacherId FROM attendance WHERE id = #{Attid} ")
    int queryTeacherId(@Param("Attid") int Attid);
    /**
     * 添加申诉信息
     * @param stuid
     * @param time
     * @param Attid
     * @param coursename
     * @param description
     * @param imagePath
     */
    @Insert("insert INTO attendance_sheet VALUES (DEFAULT,#{stuid},#{teacherId},#{time},#{Attid},#{changeStatus},0,0,#{coursename},null,#{description},#{imagePath},#{stuid},NOW(),#{stuid},NOW())")
    void insertStuAttendanceInfo(@Param("stuid") String stuid,@Param("time") String time,@Param("teacherId") int teacherId,@Param("changeStatus") int changeStatus,@Param("Attid") int Attid ,@Param("coursename") String coursename, @Param("description") String description,@Param("imagePath") String imagePath);

    @Update("update attendance_sheet set isread = 1 where id = #{id}")
    void updateIsread(@Param("id") int id);

    /**
     * 教师查询自己班的申诉信息
     * @param username
     * @return
     */
//    @Select("SELECT x.*, y.status from  (select  a.id,a.stuid,a.create_time,c.cname,s.sname,a.isread,a.Attid from attendance_sheet a\n" +
//            " INNER JOIN student_info s ON a.stuid = s.stuid INNER JOIN class_info c ON s.cid = c.id \n" +
//            "where teacherId = (SELECT id from systemuser where username = #{username}) ) x \n" +
//            "INNER JOIN attendance_records y ON x.Attid = y.attId AND x.stuid = y.stuid ORDER BY x.isread asc,x.create_time DESC")
    List<Map<String,Object>> queryStuAttendanceInfoByTea(@Param("username") String username );

    /**
     * 查询内容,图片路径
     * @param id
     * @return
     */
    @Select("select a.description,a.imagePath,a.coursename,a.stuid,e.classtime,e.date,a.changeStatus from attendance_sheet a INNER JOIN attendance t on a.Attid = t.id INNER JOIN `schedule` e\n" +
            "ON t.scheduleId = e.id  where a.id = #{id} ")
    Map<String,Object> queryimagePath(@Param("id") int id);

    /**
     * 修改状态为已到
     * @param stuid
     * @param Attid
     */
    @Update("update attendance_records set status = #{status} where stuid = #{stuid} AND Attid = #{Attid}")
    void updatestatus( @Param("stuid") String stuid,@Param("Attid") int Attid ,@Param("status") int status);

    /**
     * 修改审核状态已通过
     * @param stuid
     * @param Attid
     */
    @Update("update attendance_sheet set  Auditstatus = 1 where stuid = #{stuid} AND Attid = #{Attid}")
    void updateAttidStatus(@Param("stuid") String stuid,@Param("Attid") int Attid);

    /**
     * 修改状态未通过
     * @param stuid
     * @param Attid
     * @param teacherReply
     */
    @Update("update attendance_sheet set Auditstatus = 2 , teacherReply=#{teacherReply} where stuid = #{stuid} AND Attid = #{Attid}")
    void updateAttidStatus2(@Param("stuid") String stuid,@Param("Attid") int Attid , @Param("teacherReply") String teacherReply);

    /**
     * 学生查出自己的投诉内容
     * @param id
     * @return
     */
    @Select("SELECT description,imagePath,create_time FROM attendance_sheet WHERE id = #{id}")
    Map<String ,Object> queryAttendenceImagePath( @Param("id") int id);
}
